//
//  UserRouteHandler.m
//  JavaScriptAndObjectiveC
//
//  Created by 刘红武 on 2018/4/25.
//  Copyright © 2018年 huangyibiao. All rights reserved.
//

#import "UserRouteHandler.h"
#import "ViewController.h"
@implementation UserRouteHandler
+(void)load {
    [[KBRouter shard] registerHandler:[[UserRouteHandler alloc] init] forRoute:@"UserRouteHandler"];
}
- (void)RealHappenWithParams:(NSMutableDictionary *)param {
    
    NSString *service = param[@"service"];//处理器
    NSString *action = param[@"action"];  //响应
    NSString *pam = param[@"param"];   //参数
    NSString *successCallback = param[@"successCallback"];  //成功回调
    NSString *errorCallback = param[@"errorCallback"];    //失败回调
    
    if ([action isEqualToString:@"pushIvew"]) {
        
        ViewController *vc = [[ViewController alloc] init];
        NSURL *url = [[NSBundle mainBundle] URLForResource:@"test1" withExtension:@"html"];
        vc.MainUrl = url;
        UIViewController *topVC = [self topViewController];
        if (![topVC isKindOfClass:[UINavigationController class]]) {
            [topVC presentViewController:vc animated:YES completion:nil];
            
        }
        else if ([topVC isKindOfClass:[UINavigationController class]]){
            UINavigationController * nav = (UINavigationController *)topVC;
            
            [nav pushViewController:vc animated:YES];
        }
        
    }else if ([action isEqualToString:@"login"]) {
        
        
    }else if ([action isEqualToString:@"seting"]) {
        
        
    }
    
}
- (UIViewController *)topViewController {
    UIViewController *resultVC;
    resultVC = [self _topViewController:[[UIApplication sharedApplication].keyWindow rootViewController]];
    while (resultVC.presentedViewController) {
        resultVC = [self _topViewController:resultVC.presentedViewController];
    }
    return resultVC;
}

- (UIViewController *)_topViewController:(UIViewController *)vc {
    if ([vc isKindOfClass:[UINavigationController class]]) {
        return [self _topViewController:[(UINavigationController *)vc topViewController]];
    } else if ([vc isKindOfClass:[UITabBarController class]]) {
        return [self _topViewController:[(UITabBarController *)vc selectedViewController]];
    } else {
        return vc;
    }
    return nil;
}
@end
